/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kcs.common.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Locale;


/**
 *
 * @author BeeZa
 */
public class ConnectionUtils {

    private static Connection connection = null;

    public ConnectionUtils() {
    	
    	Locale locale = Locale.getDefault(); 
    	 
    	locale = Locale.US;
    	Locale.setDefault(locale);

    }

    public static Connection getConnection(String base, String from_to){

        String driver = PropsUtils.getMessage(Constant.PROP_DATABASE, "db."+ base +".driver");
        String url = PropsUtils.getMessage(Constant.PROP_DATABASE, "db."+ base +".url."+ from_to);
        String username = Based64Utils.base64decode(PropsUtils.getMessage(Constant.PROP_DATABASE, "db."+ base +".login." + from_to ));
        String password = Based64Utils.base64decode(PropsUtils.getMessage(Constant.PROP_DATABASE, "db."+ base +".encpassword." + from_to));

        try {
            // Load the JDBC driver
            Class.forName(driver);
            
            // Create a connection to the database
            System.setProperty("NLS_LANG", "AMERICAN_AMERICA.TH8TISASCII");
            connection = DriverManager.getConnection(url, username, password);

            connection.setAutoCommit(false);

        } catch (ClassNotFoundException e) {
        	e.printStackTrace();
            LogWriter.error("Connection-1.1 " + e.getMessage(), Constant.LOGGER_COMMON);
        } catch (SQLException e) {
        	e.printStackTrace();
        	LogWriter.error("Connection-1.2 " + e.getMessage(), Constant.LOGGER_COMMON);
        }
        return connection;
    }
    
    /*public static Connection getConnection(String base, String from_to){
  
    	String dsName = PropsUtils.getMessage(Constant.PROP_DATABASE, "ds."+ base + "." + from_to);

			try {
				Context initialContext = new InitialContext();
				DataSource datasource = (DataSource) initialContext.lookup("java:"+dsName);
				connection = datasource.getConnection();
				connection.setAutoCommit(false);
				
			} catch (Exception e) {
				e.printStackTrace();
				LogWriter.error("Connection Pass DataSource-1.1 " + e.getMessage(), Constant.LOGGER_COMMON);
			}
	

		return connection;
	}*/

    
    public static void closeConnection(Connection conn){
    	if (conn != null){
    		try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				// TODO Auto-generated catch block
				LogWriter.error("Can not close connection : " + e.getMessage(), Constant.LOGGER_COMMON);
			}
    	}
    }
    
    public static void main(String args[]) {
    	for (int i = 0; i < 1; i++) {
    		Connection con = getConnection(Constant.ORACLE, Constant.FROM);
            if (con != null){
            	System.out.println((i+1) + "Yes");
            }
		}
        
    }   
}

